﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;


namespace Perpus
{
    public partial class PinjamBuku : Form
    {
        public PinjamBuku()
        {
            InitializeComponent();
        }
        NhibernateHelper help = new NhibernateHelper();

        private void PinjamBuku_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dotnetDataSet.Anggota' table. You can move, or remove it, as needed.
            this.anggotaTableAdapter.Fill(this.dotnetDataSet.Anggota);
            // TODO: This line of code loads data into the 'dotnetDataSet.Pegawai' table. You can move, or remove it, as needed.
            this.pegawaiTableAdapter.Fill(this.dotnetDataSet.Pegawai);
            // TODO: This line of code loads data into the 'dotnetDataSet.Buku' table. You can move, or remove it, as needed.
            this.bukuTableAdapter.Fill(this.dotnetDataSet.Buku);
            // TODO: This line of code loads data into the 'cobaDataSet.Buku' table. You can move, or remove it, as needed.


            var factory = help.CreateSessionFactory();
            //int idPinjam = Convert.ToInt32();
            try
            {
                using (var session = factory.OpenSession())
                {

                    var id_borrow = from c in session.Query<Peminjaman>()
                                    select c.id_peminjaman;

                    string tes = id_borrow.Max().ToString();
                    label7.Text = tes;

                }
            }
            catch
            {

            }
            

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;
            kodeBuku_textBox1.Text = dataGridView1[0, i].Value.ToString();
            judulBuku_textBox2.Text = dataGridView1[1, i].Value.ToString();
            status_textBox3.Text = dataGridView1[2, i].Value.ToString();
            denda_textBox4.Text = dataGridView1[3, i].Value.ToString();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            var factory = help.CreateSessionFactory();
            int idpegawai = Convert.ToInt32(comboBox1.SelectedValue.ToString());
            int idanggota = Convert.ToInt32(comboBox2.SelectedValue.ToString());
            int kodBuk = Convert.ToInt32(kodeBuku_textBox1.Text);

                try
                {
                    using (var session = factory.OpenSession())
                    {
                        if (status_textBox3.Text != "dipinjam")
                        {
                            using (var transaction = session.BeginTransaction())
                            {
                                var borrow = new Peminjaman
                                {
                                    id_pegawai = session.Get<Pegawai>(idpegawai),
                                    nomor_registrasi = session.Get<Anggota>(idanggota),
                                    tanggal_peminjaman = Convert.ToDateTime(dateTimePicker1.Value.ToString())
                                };

                                //Buku mapBuk = session.Get<Buku>(kodBuk);
                                //mapBuk.status = "dipinjam";

                                session.Save(borrow);
                                transaction.Commit();

                                MessageBox.Show("Berhasil!");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Buku Sedang Dipinjam");
                        }
                    }
                }
                catch(TransactionException m)
                {
                    MessageBox.Show(m.ToString());
                }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            admin_MenuAwal menu = new admin_MenuAwal();
            menu.Show();
            this.Hide();
        }

        private void button3_Click(object sender, EventArgs e)
        {
          

        }
    }
}
